package com.tiger.db.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tiger.db.entity.DictionaryCategory;
import com.tiger.db.entity.DictionaryItem;
import com.tiger.db.mapper.DictionaryItemMapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DictionaryItemService extends BaseService<DictionaryItemMapper, DictionaryItem> {
    private final DictionaryCategoryService dictionaryCategoryService;

    public DictionaryItemService(DictionaryCategoryService dictionaryCategoryService) {
        this.dictionaryCategoryService = dictionaryCategoryService;
    }

    public List<DictionaryItem> getItemsByCategory(String category) {
        List<DictionaryItem> list = new ArrayList<>();
        DictionaryCategory cate = dictionaryCategoryService.getOne(new QueryWrapper<DictionaryCategory>().select("id").eq("code", category));
        if(cate != null) {
            list = this.list(new QueryWrapper<DictionaryItem>().eq("category_id", cate.getId()));
        }
        return list;
    }
}